package cn.iocoder.yudao.module.member.controller.admin.userpromotionrecords;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userpromotionrecords.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpromotionrecords.UserPromotionRecordsDO;
import cn.iocoder.yudao.module.member.service.userpromotionrecords.UserPromotionRecordsService;

@Tag(name = "管理后台 - 用户推广记录")
@RestController
@RequestMapping("/member/user-promotion-records")
@Validated
public class UserPromotionRecordsController {

    @Resource
    private UserPromotionRecordsService userPromotionRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户推广记录")
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:create')")
    public CommonResult<Long> createUserPromotionRecords(@Valid @RequestBody UserPromotionRecordsSaveReqVO createReqVO) {
        return success(userPromotionRecordsService.createUserPromotionRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户推广记录")
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:update')")
    public CommonResult<Boolean> updateUserPromotionRecords(@Valid @RequestBody UserPromotionRecordsSaveReqVO updateReqVO) {
        userPromotionRecordsService.updateUserPromotionRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户推广记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:delete')")
    public CommonResult<Boolean> deleteUserPromotionRecords(@RequestParam("id") Long id) {
        userPromotionRecordsService.deleteUserPromotionRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户推广记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:query')")
    public CommonResult<UserPromotionRecordsRespVO> getUserPromotionRecords(@RequestParam("id") Long id) {
        UserPromotionRecordsDO userPromotionRecords = userPromotionRecordsService.getUserPromotionRecords(id);
        return success(BeanUtils.toBean(userPromotionRecords, UserPromotionRecordsRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得用户推广记录分页")
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:query')")
    public CommonResult<PageResult<UserPromotionRecordsRespVO>> getUserPromotionRecordsPage(@Valid @RequestBody UserPromotionRecordsPageReqVO pageReqVO) {
        return success(userPromotionRecordsService.getUserPromotionRecordsPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户推广记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-promotion-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPromotionRecordsExcel(@Valid UserPromotionRecordsPageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPromotionRecordsRespVO> list = userPromotionRecordsService.getUserPromotionRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户推广记录.xls", "数据", UserPromotionRecordsRespVO.class, list);
    }

}